﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ValuationResource;
using ValuationLogic;

namespace ValuationUI
{
    /// <summary>
    /// Interaction logic for RegisterForm.xaml
    /// </summary>
    public partial class RegisterForm : Window
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void registerBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.userNameTextBox.Text.Trim().Length == 0)
            {
                MessageBox.Show(this, "User name can't be null");
                return;
            }
            if (this.passwordBox.Password.Trim().Length == 0 || this.passwordConfirmBox.Password.Trim().Length == 0)
            {
                MessageBox.Show(this, "Password can't be null");
                return;
            }
            if (this.passwordBox.Password != this.passwordConfirmBox.Password)
            {
                MessageBox.Show(this, "The passwords are different");
                this.passwordBox.Clear();
                this.passwordConfirmBox.Clear();
                return;
            }
            User newUser = new User(this.userNameTextBox.Text, this.passwordBox.Password);
            #region 注册用户，如果成功
            if (Logic.addUser(newUser))
            {
                LoginForm newLogin = new LoginForm();
                newLogin.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Fail to add user!");
                return;
            }
            #endregion

        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            LoginForm newLogin = new LoginForm();
            newLogin.Show();
            this.Close();
        }
    }
}
